﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using STLGROUP.Layers.DataAccessLayers;
using STLGROUP.Layers.Common;

namespace QuanLyBanHang.SystemForms
{
    public partial class frmResetPassword : STLGROUP.Layers.Presentations.FormBase.frmBase
    {
        #region Constructors

        public frmResetPassword()
        {
            InitializeComponent();

            this.Load += new EventHandler(frmResetPassword_Load);            
            cbxEmployeeID.SelectedValueChanged += new EventHandler(cbxEmployeeID_SelectedValueChanged);
            btxResetPassword.Click += new EventHandler(btxResetPassword_Click);
            btxExit.Click += new EventHandler(btxExit_Click);
        }

        #endregion

        #region Private Methods

        #region LoadDataToCombobox

        private void LoadDataToCombobox()
        {
            try
            {
                var q = (from r in Cache.Context.DT_NhanVien
                         select new
                         {
                             r.IDNhanVien,
                             r.MaNhanVien,
                             HoTenNhanVien = r.MaNhanVien + " - " + r.HoVaTenDem + " " + r.TenNhanVien,
                         }).ToList().ConvertAll(s => new View_NhanVienLocal
                         {
                             IDNhanVien = s.IDNhanVien,
                             MaNhanVien = s.MaNhanVien,
                             HoTenNhanVien = s.HoTenNhanVien
                         });
                if (q.ToList().Count > 0)
                {
                    cbxEmployeeID.DataSource = q.ToList();
                    cbxEmployeeID.DisplayMember = "HoTenNhanVien";
                    cbxEmployeeID.ValueMember = "IDNhanVien";
                }
                else
                {
                    cbxEmployeeID.Items.Clear();
                }
            }
            catch (Exception)
            {

            }
        }

        #endregion

        #region ShowUserAccountInformation

        private void ShowUserAccountInformation(int employeeID)
        {
            try
            {
                var data = (from tk in Cache.Context.ACL_TaiKhoan
                            join nv in Cache.Context.DT_NhanVien on tk.IDNhanVien equals nv.IDNhanVien
                            join nhom in Cache.Context.ACL_NhomNguoiDung on tk.IDNhomNguoiDung equals nhom.IDNhomNguoiDung
                            where nv.IDNhanVien == employeeID
                            select new
                            {
                                HoTenNhanVien = nv.HoVaTenDem + nv.TenNhanVien,
                                TenDangNhap = tk.TenDangNhap,
                                TenNhom = nhom.TenNhom
                            }).FirstOrDefault();

                if (data != null)
                {
                    txtFullName.Text = data.HoTenNhanVien;
                    txtUserGroup.Text = data.TenNhom;
                    txtUserName.Text = data.TenDangNhap;
                }
            }
            catch (Exception)
            {
                txtFullName.Text = txtUserName.Text = txtUserGroup.Text = "Mã nhân viên không tồn tại";
            }
        }

        #endregion

        #region ResetPassword

        private void ResetPassword(int userAccountID)
        {
            try
            {
                var userAccount = Cache.Context.ACL_TaiKhoan.Where(user => user.IDNhanVien == userAccountID).FirstOrDefault();

                if (userAccount != null)
                {
                    userAccount.MatKhau = UICommon.Encryption(userAccount.MaNhanVien.ToString());
                    userAccount.GoiYMatKhau = "";
                    Cache.Context.SaveChanges();

                    MessageBox.Show("Reset mật khẩu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Reset mật khẩu không thành công", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch
            {
#if DEBUG
                // MessageBox.Show(ex.Message, "Lỗi");
#else
                MessageBox.Show("Reset mật khẩu không thành công", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
            }
        }

        #endregion

        #endregion

        #region Events

        private void btxExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btxResetPassword_Click(object sender, EventArgs e)
        {
            int employeeID;
            if (cbxEmployeeID.SelectedValue != null)
            {
                if (int.TryParse(cbxEmployeeID.SelectedValue.ToString(), out employeeID))
                {
                    ResetPassword(employeeID);
                }
                else
                {
                    MessageBox.Show("Mã nhân viên không hợp lệ", "Lỗi");
                }
            }
        }

        private void cbxEmployeeID_SelectedValueChanged(object sender, EventArgs e)
        {
            int idNhanVien;
            if (cbxEmployeeID.SelectedValue != null)
            {
                if (int.TryParse(cbxEmployeeID.SelectedValue.ToString(), out idNhanVien))
                {
                    ShowUserAccountInformation(idNhanVien);
                }
            }
            else
            {
                txtFullName.Text = txtUserName.Text = txtUserGroup.Text = "Mã nhân viên không hợp lệ";
            }
        }

        private void frmResetPassword_Load(object sender, EventArgs e)
        {
            LoadDataToCombobox();
        }

        public class View_NhanVienLocal
        {
            public int IDNhanVien { get; set; }
            public string MaNhanVien { get; set; }
            public string HoTenNhanVien { get; set; }
        }

        #endregion
    }
}
